package com.ta.stage.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ta.stage.dao.DeptDao;
import com.ta.stage.entity.Dept;
import com.ta.stage.entity.vo.DeptVo;
import com.ta.stage.result.Result;
import com.ta.stage.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/dept/")
@Api(tags={"部门 DeptCOntroller"})
public class DeptController extends BaseController {

    @Autowired
    DeptService deptService;


    @ApiOperation(value = "查询部门列表")
    @RequestMapping(value = "findDept",method = RequestMethod.GET)
    @ResponseBody
    public Result findDept(){
        List<Dept> deptList = deptService.list(null);
        return Result.ok(deptList);
    }

    @ApiOperation(value = "根据id查询部门")
    @GetMapping("getDept/{id}")
    public Result getDept(@PathVariable Long id){
        Dept dept = deptService.getById(id);
        return Result.ok(dept);
    }

    @ApiOperation(value = "新增部门列表")
    @RequestMapping(value = "saveDept",method = RequestMethod.POST)
    @ResponseBody
    public Result saveDept(@RequestBody Dept dept){
         boolean save = deptService.save(dept);
         return save?Result.ok():Result.fail();
    }

    @ApiOperation(value = "逻辑删除部门")
    @DeleteMapping("{id}")
    public Result removeDept(@PathVariable Long id){
        boolean flag = deptService.removeById(id);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "修改部门设置")
    @PostMapping("updateDept")
    public Result updateDept(@RequestBody Dept dept){
        boolean flag = deptService.updateById(dept);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "批量删除部门设置")
    @DeleteMapping("batchRemoveDept")
    public Result batchRemoveDept(@RequestBody List<Long> idList){
        deptService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "部门条件查询带分页")
    @PostMapping("findPageDept/{current}/{limit}")
    public Result findPageDept(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) DeptVo deptVo){

        //创建page对象，传递当前页，每页记录数
        Page<Dept> page = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
             Integer id = deptVo.getId();
             String name = deptVo.getName();
             String charge = deptVo.getCharge();
             String address = deptVo.getAddress();
        //条件可能为空
        if (!StringUtils.isEmpty(id)){
            wrapper.eq("id",deptVo.getId());
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",deptVo.getName());
        }
        if (!StringUtils.isEmpty(charge)){
            wrapper.like("charge",deptVo.getCharge());
        }
        if (!StringUtils.isEmpty(address)){
            wrapper.like("address",deptVo.getAddress());
        }
        //调用方法实现分页查询
        Page<Dept> pageDept = deptService.page(page, wrapper);
        return Result.ok(pageDept);
    }
}
